﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Presenters;
using University.Workshop.Model;
using System.Collections.ObjectModel;

namespace University.Workshop.WinApp.Arching
{
    public partial class ArchingView : Form,IRent
    {
        static int indexData;
        UserLogOn user = UserLogOn.Instance;
        private RegistrationRentPresenter rentPresenter;
        public ArchingView()
        {
            InitializeComponent();
            rentPresenter = new RegistrationRentPresenter(this);
            indexData = 0;
            rentPresenter.OnInitializeFormGetRentCode();
        }

        private void ArchingView_Load(object sender, EventArgs e)
        {
            radioButtonWater.Checked = true;
            textBoxCategory.Text = radioButtonWater.Text;
        }

        #region SendParameter
        public string RentCode { get { return textBoxRentCode.Text; } set { textBoxRentCode.Text = value; } }
        public string UserCode { get { return user.Id; } }
        public DateTime DateRent { get { return dateTimePickerRentDate.Value; } }
        public string Concept { get { return textBoxConcept.Text; } }
        public string Category { get { return textBoxCategory.Text; } set { textBoxCategory.Text = value; } }
        public string TotalAmount { get { return labelTotalAmount.Text; } }
        public Collection<RentDetail> ListOfAmounts { get { return ReturnAllSelectAmounts(); } }
   
        public string Amount { get { return textBoxAmounts.Text; } }
        public string Note { get { return textBoxNotes.Text; } }

        private Collection<RentDetail> ReturnAllSelectAmounts()
        {
            Collection<RentDetail> list = new Collection<RentDetail>();
            for (int i = 0; i < dataGridViewRent.Rows.Count; i++)
            {
                RentDetail rentProduct = new RentDetail();
                rentProduct.CodeRent = textBoxRentCode.Text;
                rentProduct.Amount = Convert.ToDouble(dataGridViewRent.Rows[i].Cells[0].Value.ToString(), System.Globalization.CultureInfo.CurrentCulture);
                rentProduct.Note = dataGridViewRent.Rows[i].Cells[1].Value.ToString();
                list.Add(rentProduct);
            }
            return list;
        }
        #endregion

        #region radioButtons events
        private void radioButtonWater_CheckedChanged(object sender, EventArgs e)
        {
            textBoxCategory.Text = radioButtonWater.Text;
        }

        private void radioButtonRent_CheckedChanged(object sender, EventArgs e)
        {
            textBoxCategory.Text = radioButtonRent.Text;
        }

        private void radioButtonElectricity_CheckedChanged(object sender, EventArgs e)
        {
            textBoxCategory.Text = radioButtonElectricity.Text;
        }

        private void radioButtonInternetService_CheckedChanged(object sender, EventArgs e)
        {
            textBoxCategory.Text = radioButtonInternetService.Text;
        }

        private void radioButtonTransport_CheckedChanged(object sender, EventArgs e)
        {
            textBoxCategory.Text = radioButtonTransport.Text;
        }

        private void radioButtonOthers_CheckedChanged(object sender, EventArgs e)
        {
            textBoxCategory.Text = radioButtonOthers.Text;
        }
        #endregion

        private void buttonAddToList_Click(object sender, EventArgs e)
        {
            string resp = rentPresenter.OnAddAnAmountToList();
            if (resp == "Todo esta bien")
            {
                dataGridViewRent.Rows.Add();
                dataGridViewRent.Rows[indexData].Cells[0].Value = textBoxAmounts.Text;
                dataGridViewRent.Rows[indexData].Cells[1].Value = textBoxNotes.Text;
                indexData++;
                labelTotalAmount.Text = (Convert.ToDouble(labelTotalAmount.Text) + Convert.ToDouble(textBoxAmounts.Text)).ToString();
                textBoxAmounts.Clear();
                textBoxNotes.Clear();
            }
            else
                MessageBox.Show(this, resp, "Error al añadir producto", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
        }

        private void buttonRegister_Click(object sender, EventArgs e)
        {
            string resp = rentPresenter.OnRegisterRent();
            if (resp == "Registro exitoso!")
            {
                MessageBox.Show(this, resp, "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                this.Close();
            }
            else
                MessageBox.Show(this, resp, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
        }

        private void textBoxAmounts_KeyPress(object sender, KeyPressEventArgs e)
        {
            Validator.ValidateOnlyFloat(sender, e);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
